<template>
  <div id="statistcsBox">
    <Sakura></Sakura>
    <van-sticky>
      <!-- 头部导航栏 -->
      <NavTop>
        <template #left>
          <van-icon name="arrow-left animate__animated animate__fadeInLeft" @click="leftFn" />
        </template>
      </NavTop>
    </van-sticky>
    <!-- 内容 -->
    <div class="box">
      <div class="box-top">
        <h5>我们</h5>
        <span
          >相遇的第&nbsp;
          <p>{{ nowTime }}</p>
          &nbsp;天</span
        >
      </div>
      <div class="box-footer">
        <div class="foot-one">
          <span>篇数</span>
          <p>{{ articleNub }}</p>
        </div>
        <div class="foot-one">
          <span>文字</span>
          <p>{{ textNub }}</p>
        </div>
        <div class="foot-one">
          <span>图片</span>
          <p>{{ imgNub }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavTop from '@/components/home/homenNavTop.vue'
import Sakura from '@/components/Sakura.vue'
export default {
  name: 'statisticsIndex',
  data () {
    return {
      // 用户日记数据
      userDiaryList: [],
      // 用户注册到现在多少天了
      nowTime: '',
      // 用户日记所有的字数
      textNub: '',
      // 用户日记所有的篇数
      articleNub: '',
      // 用户日记所有的 图片数
      imgNub: ''
    }
  },
  components: {
    NavTop,
    Sakura
  },
  methods: {
    leftFn () {
      this.$router.replace('/home')
    },
    // 计算 用户注册了多少天
    datNubFn () {
      const oldTime = this.$store.state.userInfoData.time
      this.nowTime = Math.floor((new Date().getTime() - oldTime) / (1000 * 3600 * 24))
    },
    // 计算 用户所有日记 字数 / 篇数
    textNubFn () {
      if (this.userDiaryList.length === 0) {
        this.textNub = 0
        this.articleNub = 0
      } else if (this.userDiaryList !== undefined) {
        const arr = []
        for (const i in this.userDiaryList) {
          arr.push(this.userDiaryList[i].tilength)
        }

        // eslint-disable-next-line no-eval
        this.textNub = eval(arr.join('+'))
        this.articleNub = this.userDiaryList.length
        // console.log(this.textNub, this.articleNub)
      }
    },
    // 计算 用户所有日记 图片 数
    imgNubFn () {
      if (this.userDiaryList === undefined) {
        this.imgNub = 0
      } else {
        const imgArr = []
        for (const i in this.userDiaryList) {
          // console.log(this.userDiaryList[i].imgurl === null)
          if (this.userDiaryList[i].imgurl === null) {
            this.imgNub = 0

            // console.log(this.userDiaryList[i].imgurl.split('||'))
          } else {
            imgArr.push(this.userDiaryList[i].imgurl.split('||').length)
          }
        }
        // eslint-disable-next-line no-eval
        this.imgNub = eval(imgArr.join('+'))
      }
    }
  },
  activated () {
    this.$store.dispatch('upUserDiaryList')
  },
  mounted () {
    this.userDiaryList = this.$store.state.userDiaryList
    // console.log(this.userDiaryList)
    this.textNubFn()
    this.imgNubFn()
    this.datNubFn()
  }
}
</script>

<style lang="less" scoped>
#statistcsBox {
  height: 100vh;
}
.top[data-v-a967a83e]{
  background-color: #f7f8fa00;
}
.box {
  // width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 30px;
  .box-top {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 50px 0;
    h5 {
      font-size: 26px;
      letter-spacing: 2px;
      color: #646566;
    }
    span {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 25px;
      font-size: 18px;
      color: #777;
      letter-spacing: 2px;
      p {
        font-size: 24px;
      }
    }
  }
  .box-footer {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    border-top: 1px solid #ededed;
    border-bottom: 1px solid #ededed;
    padding: 30px 0;

    .foot-one {
      display: flex;
      flex-direction: column;
      align-items: center;
      span {
        font-size: 16px;
        color: #969799;
        padding-bottom: 15px;
        letter-spacing: 2px;
      }
      p {
        font-size: 22px;
        color: #646566;
      }
    }
  }
}
</style>
